package com.example.poisonous;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.Toast;

import com.example.poisonous.entity.PersonBean;

import cn.bmob.v3.BmobQuery;
import cn.bmob.v3.exception.BmobException;
import cn.bmob.v3.listener.QueryListener;
import cn.bmob.v3.listener.SaveListener;
import cn.bmob.v3.listener.UpdateListener;

public class MainActivity extends AppCompatActivity implements View.OnClickListener {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        initView();
    }

    private void initView() {
        Button mButton_Add = findViewById(R.id.mButton_Add);
        mButton_Add.setOnClickListener(this);

        Button mButton_Query = findViewById(R.id.mButton_Query);
        mButton_Query.setOnClickListener(this);

        Button mButton_Modify = findViewById(R.id.mButton_Modify);
        mButton_Modify.setOnClickListener(this);

        Button mButton_Delete = findViewById(R.id.mButton_Delete);
        mButton_Delete.setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.mButton_Add:
                PersonBean p2 = new PersonBean();
                p2.setName("lucky");
                p2.setAddress("北京海淀");
                p2.save(new SaveListener<String>() {
                    @Override
                    public void done(String objectId, BmobException e) {
                        if (e == null) {
                            Toast.makeText(MainActivity.this, "添加数据成功，返回objectId为：" + objectId, Toast.LENGTH_SHORT).show();
                        } else {
                            Toast.makeText(MainActivity.this, "创建数据失败：" + e.getMessage(), Toast.LENGTH_SHORT).show();
                        }
                    }
                });
                break;

            case R.id.mButton_Query:
                //查找Person表里面id为6b6c11c537的数据
                BmobQuery<PersonBean> bmobQuery = new BmobQuery<>();
                bmobQuery.getObject("0b6410953c", new QueryListener<PersonBean>() {
                    @Override
                    public void done(PersonBean object, BmobException e) {
                        if (e == null) {
                            Toast.makeText(MainActivity.this, "查询成功" + "用户名:" + object.getName() + "地址:" + object.getAddress(), Toast.LENGTH_SHORT).show();
                        } else {
                            Toast.makeText(MainActivity.this, "查询失败:" + e.getMessage(), Toast.LENGTH_SHORT).show();
                        }
                    }
                });
                break;

            case R.id.mButton_Modify:
                //更新Person表里面id为6b6c11c537的数据，address内容更新为“北京朝阳”
                final PersonBean moP2 = new PersonBean();
                moP2.setAddress("北京朝阳");
                moP2.update("0b6410953c", new UpdateListener() {
                    @Override
                    public void done(BmobException e) {
                        if (e == null) {
                            Toast.makeText(MainActivity.this, "更新成功:" + moP2.getUpdatedAt(), Toast.LENGTH_SHORT).show();
                        } else {
                            Toast.makeText(MainActivity.this, "更新失败：" + e.getMessage(), Toast.LENGTH_SHORT).show();
                        }
                    }
                });
                break;

            case R.id.mButton_Delete:
                final PersonBean DeP2 = new PersonBean();
                DeP2.setObjectId("ae4bc54ce3");
                DeP2.delete(new UpdateListener() {
                    @Override
                    public void done(BmobException e) {
                        if (e == null) {
                            Toast.makeText(MainActivity.this, "删除成功:" + DeP2.getUpdatedAt(), Toast.LENGTH_SHORT).show();
                        } else {
                            Toast.makeText(MainActivity.this, "删除失败：" + e.getMessage(), Toast.LENGTH_SHORT).show();
                        }
                    }
                });
                break;
        }
    }
}
